package com.tianji.learning.listener;

import com.tianji.api.dto.remark.LikedTimesDTO;
import com.tianji.common.constants.MqConstants;
import com.tianji.common.utils.JsonUtils;
import com.tianji.learning.service.IInteractionReplyService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

@Slf4j
@Component
public class OnReplyTimeChangeListener {

    @Autowired
    private IInteractionReplyService replyService;

    /**
     * 接收点赞微服务发出的点赞数量变更的消息
     * */
    @RabbitListener(bindings = @QueueBinding(
            value=@Queue(MqConstants.Queue.QA_LIKED_TIMES_QUEUE),
            exchange = @Exchange(value = MqConstants.Exchange.LIKE_RECORD_EXCHANGE, type = ExchangeTypes.TOPIC),
            key = MqConstants.Key.QA_LIKED_TIMES_KEY
    ))
    public void onReplyTimeChangeListener(List<LikedTimesDTO> dtos ){
        log.info("点赞微服务发出的点赞数量变更的消息:{}", JsonUtils.toJsonStr(dtos));
        // 更新reply的点赞数量
        replyService.updateLikeTime(dtos);
    }

}
